﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Fin;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.Fin.ReceivableVMs
{
    public partial class ReceivableListVM : BasePagedListVM<Receivable_View, ReceivableSearcher>
    {
        
        protected override IEnumerable<IGridColumn<Receivable_View>> InitGridHeader()
        {
            return new List<GridColumn<Receivable_View>>{
                
                this.MakeGridHeader(x => x.Receivable_ReceivableCode).SetTitle(@Localizer["Page.收款单号"].Value),
                this.MakeGridHeader(x => x.Receivable_Customer).SetTitle(@Localizer["Page.客户"].Value),
                this.MakeGridHeader(x => x.Receivable_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.Receivable_ReceivableDate).SetTitle(@Localizer["Page.收款日期"].Value),
                this.MakeGridHeader(x => x.Receivable_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.Receivable_SettlementType).SetTitle(@Localizer["Page.结算方式"].Value),
                this.MakeGridHeader(x => x.Receivable_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.Receivable_TotalPrice).SetTitle(@Localizer["Page.实收金额"].Value),
                this.MakeGridHeader(x => x.Receivable_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.Receivable_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Receivable_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Receivable_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Receivable_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.Receivable_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),
                

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<Receivable_View> GetSearchQuery()
        {
            var query = DC.Set<Receivable>()
                
                .CheckContain(Searcher.ReceivableCode, x=>x.ReceivableCode)
                .CheckEqual(Searcher.CustomerId, x=>x.CustomerId)
                .CheckEqual(Searcher.MakeUserId, x=>x.MakeUserId)
                .CheckBetween(Searcher.ReceivableDate?.GetStartTime(), Searcher.ReceivableDate?.GetEndTime(), x => x.ReceivableDate, includeMax: false)
                .CheckEqual(Searcher.SettlementAccountId, x=>x.SettlementAccountId)
                .CheckEqual(Searcher.SettlementTypeId, x=>x.SettlementTypeId)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckEqual(Searcher.TotalPrice, x=>x.TotalPrice)
                .CheckContain(Searcher.Remark, x=>x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new Receivable_View
                {
				    ID = x.ID,
                    
                    Receivable_ReceivableCode = x.ReceivableCode,
                    Receivable_Customer = DC.Set<Customer>().Where(z0 => z0.CustomerCode == x.CustomerId).Select(y => y.CustomerName).FirstOrDefault(),
                    Receivable_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    Receivable_ReceivableDate = x.ReceivableDate,
                    Receivable_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    Receivable_SettlementType = x.SettlementType.DicFieldDes,
                    Receivable_Status = x.Status,
                    Receivable_TotalPrice = x.TotalPrice,
                    Receivable_Remark = x.Remark,
                    Receivable_CreateTime = x.CreateTime,
                    Receivable_UpdateTime = x.UpdateTime,
                    Receivable_CreateBy = x.CreateBy,
                    Receivable_UpdateBy = x.UpdateBy,
                    Receivable_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    public override void AfterDoSearcher()
    {
        if (Wtm.LoginUserInfo != null)
        {
            var ids = GetMyApproves();
            foreach (var item in EntityList)
            {
                var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                if (m!=null)
                {
                    item.wtmtemp_CanApprove = true;
                }
            }
        }
    }


    }
    public class Receivable_View: Receivable
    {
        
        public string Receivable_ReceivableCode { get; set; }
        public string Receivable_Customer { get; set; }
        public string Receivable_MakeUser { get; set; }
        public DateTime? Receivable_ReceivableDate { get; set; }
        public string Receivable_SettlementAccount { get; set; }
        public string Receivable_SettlementType { get; set; }
        public StatusTypeEnum? Receivable_Status { get; set; }
        public decimal? Receivable_TotalPrice { get; set; }
        public string Receivable_Remark { get; set; }
        public DateTime? Receivable_CreateTime { get; set; }
        public DateTime? Receivable_UpdateTime { get; set; }
        public string Receivable_CreateBy { get; set; }
        public string Receivable_UpdateBy { get; set; }
        public bool Receivable_IsValid { get; set; }
                public bool wtmtemp_CanApprove { get; set; }

    }

}